﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;
using CustomsSystem.BLL;
using CustomsSystem.Model;

namespace WEB.T_document
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime A = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);
            int month = Convert.ToInt32(A.Date.Month.ToString());
            if (!Page.IsPostBack)
            {
                tbTimestop.Text = A.ToShortDateString();
                tbTimestart.Text = A.AddMonths(-2).Date.ToShortDateString();

                BindData();
            }
            this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
        }

        private void BindData()
        {
            SheetInfoManage sheetManage = new SheetInfoManage();
            //搜索分页数据
            //判断日期
            if (tbTimestart.Text != "" && tbTimestop.Text != "")
            {
                if (Convert.ToDateTime(tbTimestart.Text) > Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能大于开始日期！");
                    return;
                }
            }
            //判断搜索条件
            ds = sheetManage.GetView(Query());
            gvSheetInfo.DataSource = ds;
            gvSheetInfo.DataBind();
        }
        

        protected void gvSheetInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //int id = Convert.ToInt32(gvSheetInfo.SelectedRow.Cells[0].Text);
            //Response.Redirect("Up_document.aspx?ID=" + id);
        }


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 根据委托单跟踪工作流
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();
            int id = Convert.ToInt32(gvSheetInfo.DataKeys[int.Parse(index)].Value);
            Response.Redirect("Up_document.aspx?ID=" + id);
        }

        protected void gvSheetInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            
        }

        protected void gvSheetInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSheetInfo.PageIndex = e.NewPageIndex;
            gvSheetInfo.DataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
         public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {

                    case 0:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 8:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    case 7:
                        if (!PageValidate.IsNumber(tbQuery.Text.Trim())) tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;

                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            return strSql;
        }

    }
}